<script setup>
import Vrouter from "@/router"
import { onMounted, ref, inject } from 'vue'
import { search, searchWithToken } from '@/api/search/search.js'
import time from '@/utils/time/time.js'
import { MYURL } from '@/utils/express/express.js'
import { videosubscribe } from '@/api/video/video.js'
import isToken from '@/utils/token/token.js'
const router = Vrouter;
const getback = () => {
    router.push('/search')
}
const input = ref(null)
const search_text = ref('')
const search_btn = () => {
    addSearchList()
    let data
    input.value.value === '' ? data = input.value.value.placeholder : data = input.value.value
    router.push({
        path: 'search_video',
        query: { search: data }
    })
    GetSearch()
}
const addSearchList = () => {
    let local_history = JSON.parse(localStorage.getItem('history'))
    if (local_history === null) {
        local_history = []
    }
    local_history.unshift(router.currentRoute.value.query.search)
    if (local_history.length > 2) {
        for (let a = 1; a < local_history.length; a++) {
            if (local_history[0] == local_history[a]) {
                local_history.splice(a, 1)
                break
            }
        }
    }

    localStorage.setItem('history', JSON.stringify(local_history))
}
onMounted(() => {
    addSearchList()
})
const searchList = ref({
    video: [],
    user: []
})
const change_message_state = inject('change_message_state')
const subscribe = (info, index) => {
    let data = {
        account: info.account
    }
    if (!isToken()) return router.push('/login')
    videosubscribe(data).then((res) => {
        if (res.data.status != 1) {
            searchList.value.user[index].isFan = !searchList.value.user[index].isFan
        }
        change_message_state(res.data.message)
    })
}
search_text.value = router.currentRoute.value.query.search
const GetSearch = () => {
    if (!isToken()) {
        search(search_text.value).then(res => {
            searchList.value = res.data.message
        })
    } else {
        searchWithToken(search_text.value).then(res => {
            searchList.value = res.data.message
            console.log(searchList.value);
        })
    }
}
GetSearch()
</script>

<template>
    <div class="search-nav">
        <img class="back-image" src="@/assets/img/getback.png" @click="getback">
        <div class="top-nav-search">
            <img src="@/assets/img/search.png">
            <input @keydown.enter="search_btn" v-model="search_text" ref="input" type="text" placeholder="uniapp">
        </div>
        <p @click="search_btn">搜索</p>
    </div>

    <div v-show="searchList.video.length === 0 && searchList.user.length === 0">
        <IsEmpty>没有找到视频哦，换个关键词试试</IsEmpty>
    </div>
    <div v-show="searchList.video.length != 0 || searchList.user.length != 0">
        <div class="main">
            <div class="user-info" @click.stop="router.push(`/personal?up=${user.account}`)"
                v-for="(user, index)  in searchList.user" :key="index">
                <div class="user_profile">
                    <img :src="MYURL + '/public/profiles/' + user.profile">
                </div>
                <div class="user_right">
                    <div class="user_name">
                        <p>{{ user.username }}</p>
                    </div>
                    <div class="user_right_bottom">
                        <p>{{ user.fans ? user.fans.split(',').length : '-' }}粉丝</p>
                        <p>{{ user.active === 0 ? '-' : user.active }}个视频</p>
                    </div>
                </div>
                <div @click.stop="subscribe(user, index)" class="idol-right">
                    <p v-show="!user.isFan"><b style="font-size: 15px;">+</b> 关注</p>
                    <div v-show="user.isFan" class="fan">
                        <img src="@/assets/img/table.png">
                        <p>已关注</p>
                    </div>
                </div>
            </div>

            <div class="video-info" v-for="(video, index)  in searchList.video"
                @click="router.push(`videodetail?id=${video.id}`)" :key="index">
                <div class="video_profile">
                    <img :src="MYURL + '/public/videoimages/' + video.image">
                </div>
                <div class="video_info_detail">
                    <p class="video_title">{{ video.title }}</p>
                    <div class="video-author">
                        <div class="video-author-logo">
                            <img src="@/assets/img/up.png">
                            <p @click.stop="router.push(`/personal?up=${video.account}`)">{{ video.username }}</p>
                        </div>
                    </div>
                    <div class="playbacks-main">
                        <img src="@/assets/img/playbacks.png">
                        <p>3.2万</p>
                        <p>·</p>
                        <p class="video_time">{{ time(parseInt(video.time))[0] }}</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped lang="less">
.search-nav {
    margin-top: 10px;
    display: flex;
    align-items: center;

    p {
        color: var(--bilibili-pink);
        margin-left: 2%;
    }
}

.back-image {
    width: 10px;
    margin-left: 5%;
}

.top-nav-search {
    margin-left: 3%;
    display: flex;
    align-items: center;
    width: 70%;
    height: 29px;
    border: 1px #95999f solid;
    border-radius: 20px;

    input {
        margin-left: 5px;
        width: 90%;
        outline: none;
        border: none;
        font-size: 15px;
    }

    img {
        margin-left: 2%;
        width: 15px;
        object-fit: contain;
    }

    p {
        font-size: 12px;
        margin-left: 2%;
        color: #6b6f75;
    }
}

.video-info {
    margin-top: 10px;
    padding: 0 20px;
    display: flex;
    gap: 10px;
    border-bottom: .7px solid rgba(0, 0, 0, 0.12);
    padding-bottom: 10px;

    .video_info_detail {
        display: flex;
        flex-direction: column;
    }

    .video_title {
        height: 40px;
        font-size: 15px;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2; // 控制多行的行数
        -webkit-box-orient: vertical;
    }

    .video_time {
        color: #95999f;
        margin-left: 3px;
        font-size: 13px;
        margin: auto 0;
    }

    .playbacks-main {
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 2px;

        img {
            width: 15px;
        }

        p {
            color: #95999f;
            font-size: 12px;
        }
    }

    .video_profile {
        height: 100%;
        display: flex;

        img {
            width: 150px;
            height: 80px;
            object-fit: contain;
            background-color: black;
            border-radius: 5px;
        }
    }
}

.video-author {
    margin-top: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    color: #95999f;
    font-size: 14px;
    width: 100%;

    .video-author-logo {
        display: flex;
        gap: 5px;
        align-items: center;
        width: 80%;

        img {
            margin-left: 1px;
            width: 14px;
        }

        p {
            width: 100%;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            font-size: 11px;
        }
    }

    img {
        width: 12%;

    }
}

.main {
    border-top: .7px solid rgba(0, 0, 0, 0.12);
    margin-top: 20px;
}

.user-info {
    padding: 0 15px 10px 15px;
    margin-top: 15px;
    display: flex;
    gap: 15px;
    border-bottom: .7px solid rgba(0, 0, 0, 0.12);

    .user_profile {
        img {
            width: 45px;
            height: 45px;
            border-radius: 50%;
        }
    }

    .user_name {
        p {
            width: 50vw;
            white-space: nowrap;

            overflow: hidden;

            text-overflow: ellipsis;
            font-size: 13px;
        }
    }

    .user_right {
        display: flex;
        flex-direction: column;
        justify-content: space-around;

        .user_right_bottom {
            display: flex;
            gap: 10px;
            color: #95999f;
            font-size: 12px;
        }
    }

}

.idol-right {
    margin-left: auto;

    >p {
        border: solid 1px var(--bilibili-pink);
        font-size: 13px;
        color: var(--bilibili-pink);
        padding: 1px 15px;
        border-radius: 7px;
    }

    .fan {
        width: 70px;
        height: 30px;
        background-color: #e3e5e7;
        color: #95999f;
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 7px;
        gap: 3px;

        p {
            font-size: 13px;
            font-weight: 500;
        }

        img {
            width: 15px;
        }
    }
}
</style>
